package cn.ac.cintcm.prespring.tree;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

import cn.ac.cintcm.prespring.util.Config;

public class TreeStyle {
    public static final String VERTICAL = "vertical";
    public static final String HORIZONTAL = "horizontal";
    static final String SPAN_BEGIN = "<span class=\"spanTable\">";
    static final String SPAN_END = "</span>";
    
    public static StringBuilder print(String style, String name, StringBuffer tree) {
        StringBuilder treeStr = new StringBuilder();
        BufferedReader reader = new BufferedReader(new StringReader(tree.toString()));
        String line;
        treeStr.append(name);
        
        treeStr.append("= new ");
        if (style.equals(VERTICAL)) {
            treeStr.append("vTree");
        } else {
            treeStr.append("dTree");
        }
        treeStr.append("(\"");
        treeStr.append(name);
        treeStr.append("\");");
        treeStr.append(Config.NEW_LINE);
        try {
            line = reader.readLine();
            while(line != null) {
                treeStr.append(name);
                treeStr.append(".add(");
                /*if (style.equals(HORIZONTAL)) {
                    start = line.indexOf("<");
                    end = line.lastIndexOf(">");
                    if (end > start && start > 0 && end > 0) {
                        sb.setLength(0);
                        sb.append(line.substring(0, start));
                        sb.append(SPAN_BEGIN);
                        sb.append(line.substring(start, end + 1));
                        sb.append(SPAN_END);
                        sb.append(line.substring(end + 1, line.length()));
                        line = sb.toString();
                    }
                }*/
                treeStr.append(line);
                treeStr.append(");");
                treeStr.append(Config.NEW_LINE);
                line = reader.readLine();
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return treeStr;
    }
}
